package com.yanqu.road.server.manger.activity.snowgarden.service;

import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.entity.activity.snowgarden.SnowGardenUserData;
import com.yanqu.road.entity.activity.snowgarden.config.SnowGardenConfig;
import com.yanqu.road.pb.activity.QixiProto;
import com.yanqu.road.pb.activity.SnowGardenProto;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.server.game.service.IGameServiceCommand;
import com.yanqu.road.server.manger.activity.snowgarden.SnowGardenMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.sync.message.SyncResponse;

public class SnowGetUserVisitDataCmdService implements IGameServiceCommand {
    @Override
    public SyncResponse execute(SyncMessage.Request request) throws InvalidProtocolBufferException {
        SyncResponse syncResponse = new SyncResponse();
        SnowGardenProto.SnowGetOtherGameUserDataMsg reqMsg = SnowGardenProto.SnowGetOtherGameUserDataMsg.parseFrom(request.getDataBytes());


        SnowGardenProto.SnowGardenVisitUserDataFromOtherGameMsg.Builder respMsg = SnowGardenProto.SnowGardenVisitUserDataFromOtherGameMsg.newBuilder();
        respMsg.setActivityId(reqMsg.getActivityId());

        SnowGardenConfig config = SnowGardenMgr.getSnowGardenConfig();
        if (config == null || config.getActivityInfo().getActivityId() != reqMsg.getActivityId()) {
            syncResponse.setErrorCode(GameErrorCode.E_ACTIVITY_NO_FOUND);
        } else {
            for (Long userId : reqMsg.getTargetUserIdList()) {
                SnowGardenProto.SnowGardenVisitUserDataFromOtherGameTemp.Builder userTempBulider = SnowGardenProto.SnowGardenVisitUserDataFromOtherGameTemp.newBuilder();
                userTempBulider.setUserId(userId);
                userTempBulider.setScore(0);
                userTempBulider.setBuildingLevel(0);

                SnowGardenUserData targetUserSnowUserData = SnowGardenMgr.getSnowGardenUserDataOnlyGet(config.getActivityInfo().getActivityId(), userId);
                if (targetUserSnowUserData != null) {
                    userTempBulider.setScore(targetUserSnowUserData.getPeriodScore(targetUserSnowUserData.getPeriod()));
                    userTempBulider.setBuildingLevel(targetUserSnowUserData.getBuildingLevel());
                }
                respMsg.addVisitUserList(userTempBulider);
            }
        }

        syncResponse.setResponseBuilder(respMsg);
        return syncResponse;
    }
}
